package com.autotest.nore.common.data;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RandomData {
    // 常见的姓氏列表
    private static final List<String> surnames = Arrays.asList("王", "李", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴");

    // 常见的名字列表（这里以单名为例）
    private static final List<String> names = Arrays.asList("伟", "娜", "敏", "强", "静", "勇", "丽", "涛", "磊", "芳");

    // 模拟的行政区划代码列表（实际应用中可以使用更详细的列表）
    private static final List<String> areaCodes = Arrays.asList(
            "110000", "120000", "130000", "140000", "150000",
            "210000", "220000", "230000", "310000", "320000"
    );

    // 随机数生成器
    private static final Random random = new Random();

    // 生成随机索引
    private static int getRandomIndex(List<?> list) {
        return random.nextInt(list.size());
    }

    // 生成随机姓名
    public static String generateRandomName() {
        String randomSurname = surnames.get(getRandomIndex(surnames));
        String randomName = names.get(getRandomIndex(names));
        return randomSurname + randomName;
    }

    // 生成随机的6位行政区划代码
    public static String getRandomAreaCode() {
        return areaCodes.get(getRandomIndex(areaCodes));
    }

    // 生成随机的8位出生日期（格式：YYYYMMDD）
    public static String getRandomBirthDate() {
        // 假设出生年份在1950年至2020年之间
        int year = 1950 + random.nextInt(71);
        // 月份1到12
        int month = 1 + random.nextInt(12);
        // 天数1到28，避免出现非法日期
        int day = 1 + random.nextInt(28);
        return String.format("%04d%02d%02d", year, month, day);
    }

    // 生成随机的3位顺序码
    public static String getRandomSerialNumber() {
        return String.format("%03d", random.nextInt(1000));
    }

    // 计算校验码
    public static char getCheckDigit(String idNumberWithoutCheck) {
        int[] factors = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char[] checkDigits = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += Character.getNumericValue(idNumberWithoutCheck.charAt(i)) * factors[i];
        }
        return checkDigits[sum % 11];
    }

    // 生成随机的身份证号码
    public static String generateRandomIdNumber() {
        String areaCode = getRandomAreaCode();
        String birthDate = getRandomBirthDate();
        String serialNumber = getRandomSerialNumber();
        String idNumberWithoutCheck = areaCode + birthDate + serialNumber;
        char checkDigit = getCheckDigit(idNumberWithoutCheck);
        return idNumberWithoutCheck + checkDigit;
    }

    //从输入的字符串数组中随机选择一个元素并返回
    public static String arrayElement(String arrayStr){
        String[] array=arrayStr.trim().split("::");
        if (array == null || array.length == 0) {
            throw new IllegalArgumentException("Array must not be null or empty.");
        }
        Random random = new Random();
        int index = random.nextInt(array.length);
        return array[index];
    }

    // ISO 3166-1 Alpha-2 国家代码列表
    private static final List<String> alpha2Codes = Collections.unmodifiableList(Arrays.asList(
            "US", "CA", "MX", "BR", "AR", "CL", "CO", "PE", "VE", "BO",
            "PY", "SR", "UY", "EC", "GT", "HN", "NI", "PA", "SV", "CR",
            "DO", "CU", "JM", "HT", "BS", "TT", "GD", "KN", "LC", "VC",
            "AI", "AG", "BM", "VG", "KY", "TC", "MS", "BZ", "CW", "MQ",
            "GP", "MF", "BL", "PM", "AW", "SX", "DM"
            // 注意：你原始列表中有一些重复的条目，我假设这是有意为之。如果不是，请移除重复项。
    ));

    public static String getRandomAlpha2Code() {
        Random random = new Random();
        return alpha2Codes.get(random.nextInt(alpha2Codes.size()));
    }

    // ISO 3166-1 Alpha-3 国家代码列表
    private static final List<String> alpha3Codes = Collections.unmodifiableList(Arrays.asList(
            "USA", "CAN", "MEX", "BRA", "ARG", "CHL", "COL", "PER", "VEN", "BOL",
            "PRY", "SUR", "URY", "ECU", "GTM", "HND", "NIC", "PAN", "SLV", "CRI",
            "DOM", "CUB", "JAM", "HTI", "BHS", "TTO", "GRD", "KNA", "LCA", "VCT",
            "AIA", "ATG", "BMU", "VGB", "CYM", "TCA", "MSR", "BLZ", "CUW", "MTQ",
            "GLP", "MAF", "BLM", "SPM", "ABW", "SXM", "DMA"
            // 移除了重复的条目以确保唯一性
    ));

    public static String getRandomAlpha3Code() {
        Random random = new Random();
        return alpha3Codes.get(random.nextInt(alpha3Codes.size()));
    }


}
